'use strict'

import ItemComponent  from './itemComponent'
import {totalConfig} from '../config/templateConfig'

let taskConfig = totalConfig.task

//任务
class Task {
    constructor(opts) {
        opts = opts || {}
        this.receiveList = opts.receiveList || {} //领取的任务列表
        this.completeList = opts.completeList || {} //完成的任务列表
    }

    receiveTask(taskId) { //领取任务
        let taskInfo = taskConfig[taskId]
        if (taskInfo && !this.receiveList[taskId] && !this.completeList[taskId]) {
            this.receiveList[taskId] = taskId
            return 0
        } else {
            return 1
        }
    }

    completeTask(taskId, player) { //完成任务
        let taskInfo = taskConfig[taskId]
        if (taskInfo && this.receiveList[taskId]) {
            if (taskInfo.getItems) { //是否有物品
                let ret = player.bag.addItems(taskInfo.getItems)
                if (ret === -1) {
                    return 1
                }
            }
            if (taskInfo.getExp) { //是否有经验
                player.basicInfo.exp += taskInfo.getExp
            }
            if (taskInfo.getMoney) { //是否有金钱
                player.basicInfo.money += taskInfo.getMoney
            }
            this.completeList[taskId] = taskId
            delete this.receiveList[taskId]
            return 0
        } else {
            return 1
        }
    }
}
module.exports = Task
